package br.com.affero.dao;

import org.hibernate.Session;

import br.com.affero.bean.Document;
import br.com.affero.bean.User;
import br.com.affero.util.HibernateUtil;

public class DocumentDAO {
	
	private Session session;
	
	public DocumentDAO() {
		session = HibernateUtil.getInstance().getSession();
	}
	
	public void save(Document document, User user) {
		if (user != null) {
			user.getDocuments().add(document);
			document.setUser(user);
		}
		session.save(document);
	}
	
	public Document get(int id) {
		Document document = (Document) session.load(Document.class, id);
		return document;
	}
	
	public void delete(Document document) {
		document.getUser().getDocuments().remove(document);
		session.delete(document);
	}

}
